import { Navigate, Outlet, useLocation } from "react-router-dom"
import { PATHS } from "./routes"
import { tokenStore } from "@/utils/cached"
import { createLogger } from "@/utils/logger"
import kycController from "@/features_signal/kycController"
import { useEffect } from "react"

const logger = createLogger("AuthGuard")

export function AuthGuard() {
    useEffect(() => {
        kycController.fetchKyc()
    }, [])
    const token = tokenStore.get()
    const location = useLocation()
    const isPublicPath = [PATHS.INDEX, PATHS.LOGIN].includes(location.pathname)
    if (!token?.accessToken && !isPublicPath) {
        return (
            <Navigate
                to={`${PATHS.LOGIN}`}
                replace
                state={{
                    from: location.pathname,
                    search: location.search,
                    ignore: true,
                }}
            />
        )
    }

    return <Outlet />
}
